package map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class MapDemo2 {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("语文", 99);
        map.put("化学", 99);
        map.put("物理", 99);
        map.put("数学", 33);
        map.put("美术", 33);
        map.put("音乐", 33);
        //遍历键,通过map.get(key)获取值
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println(key);
            System.out.println(map.get(key));
        }
        keySet.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("keySet遍历:"+s);
            }
        });
       
        //遍历值
        Collection<Integer> values = map.values();
        for (Integer i : values) {
            System.out.println(i);

        }
        values.forEach( s-> System.out.println("values的foreach:"+s));
        //遍历键值对
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        for (Map.Entry<String, Integer> e : entrySet
        ) {
            System.out.println(e.getKey() + ":" + e.getValue());
        }

        //map直接foreach--lambda
        map.forEach((key,value)-> System.out.println("map的foreach_"+key+":"+value));

    }

}
